class Solution
{
public:
    int deleteAndEarn(vector<int>& nums)
    {
        const int N = 10001;
        int n = nums.size();
        int arr[N] = { 0 };
        for (auto x : nums) arr[x] += x;
        vector<int> f(N);
        auto g = f;
        for (int i = 1; i < N; i++)
        {
            f[i] = arr[i] + g[i - 1];
            g[i] = max(f[i - 1], g[i - 1]);
        }
        return max(f[N - 1], g[N - 1]);
    }
};